package com.hoopchina.weibo.ui;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;

import com.hoopchina.news.R;
import com.hoopchina.meta.BindItem;
import com.hoopchina.service.Networking;
import com.hoopchina.service.NewsService;
import com.hoopchina.ui.main.BaseActivity;
import com.hoopchina.ui.main.ViewManager;
import com.hoopchina.util.NetUtil;
import com.hoopchina.util.URLEncodeUtils;
import com.hoopchina.util.UrlUtils;
import com.hoopchina.util.download.DownloadCallback;
import com.hoopchina.util.download.DownloadTask;
import com.hoopchina.util.download.Downloader;
import com.hoopchina.weibo.Renren;
import com.hoopchina.weibo.Weibo;
import com.hoopchina.weibo.WeiboException;
import com.hoopchina.weibo.WeiboType;
import com.hoopchina.weibo.WeiboUtils;
import com.hoopchina.weibo.http.AccessToken;
import com.hoopchina.weibo.http.ImageItem;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 
 * @author neusoft
 * 
 */
public class ForwardingView extends BaseActivity {
	public static final String Id = "ForwardingView";
	private static final String TAG = "ForwardingView";
	public static Class<?> mOldView = null;
	public static String moldViewId = "";

	private static final int MSG_REFRESH_IMAGE_VIEW = 1;
	private static final int MSG_FORWARD_FINISH = 2;
	private static final int MSG_GET_TINY_URL_SUCCEED = 3;
	private static final int MSG_GET_TINY_URL_FAILED = 4;
	private static final int MSG_GET_FORWARD_COUNT_SUCCEED = 5;
	private static final int MSG_GET_FORWARD_COUNT_FAILED = 6;
	private static final int MAX_INPUT_NUMBER = 140;
	private static final int MAX_ABSTRACT_LENGTH = 80;
	private static final int HTTP_CONNECTION_TIMEOUT = 5000;
	private static final int LOADING_DIALOG_KEY = 100;

	private static final String CREATE_TINY_URL = "http://dwz.cn/create.php";
	private static final String GET_FORWARD_COUNT_URL = "http://newsmobile.baidu.com/n?";
	private static final String HEAD_IMAGE_SAVE_PATH = "/BindImages/";
	private EditText mEditText = null;
	private ImageView mImageView = null;
	private TextView mTextViewHeader = null;
	private TextView mBindingInfo = null;
	private TextView mRemainInputNum = null;
	private RelativeLayout mEditTextLayout = null;
	private RelativeLayout mLoadingLayout = null;
	private RelativeLayout mForwardLayout = null;
	private ProgressDialog mProgressDialog = null;
	private Button mForwardBtn = null;
	private Button mCancelBtn = null;
	private ImageItem mImageItem = null;
	private BindItem mBindItem = null;

	private String mForwardText = "";
	private String mTitle = "";
	private String mImageUrl = "";
	private String mNewsUrl = "";
	private String mTinyUrl = "";
	private String mImagePath = "";
	private String mAbs = "";
	private String mAuthor = "";
	private int mWeiboType = -1;
	private int mForwardResult = 0;
	private boolean mReturnKeyPressed = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate");
		init();
	}

	/**
	 * 消息处理
	 */
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_REFRESH_IMAGE_VIEW:
				try {
					mImageView.setVisibility(View.VISIBLE);
					Bitmap tempBitmap = BitmapFactory.decodeFile(mImagePath);
					mImageView.setImageBitmap(tempBitmap);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case MSG_FORWARD_FINISH:
				String toastMsg = "";
				if (mWeiboType == WeiboType.SinaType) {
					if (mForwardResult == -1) {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(
								mViewManager.getCurrentActivity(),
								getResources().getString(
										R.string.forward_failed),
								Toast.LENGTH_LONG).show();
					} else if (mForwardResult == 40025
							|| mForwardResult == 40028) {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(
								mViewManager.getCurrentActivity(),
								getResources().getString(
										R.string.forward_error_message_9),
								Toast.LENGTH_LONG).show();
					} else {
						sendForwardCound();
					}
				} else if (mWeiboType == WeiboType.TencentType) {
					switch (mForwardResult) {
					case 0:
						sendForwardCound();
						toastMsg = getResources().getString(
								R.string.forward_succeed);
						return;
					case 4:
						toastMsg = getResources().getString(
								R.string.forward_error_message_1);
						break;
					case 5:
						toastMsg = getResources().getString(
								R.string.forward_error_message_2);
						break;
					case 6:
						toastMsg = getResources().getString(
								R.string.forward_error_message_3);
						break;
					case 8:
						toastMsg = getResources().getString(
								R.string.forward_error_message_4);
						break;
					case 9:
						toastMsg = getResources().getString(
								R.string.forward_error_message_5);
						break;
					case 10:
						toastMsg = getResources().getString(
								R.string.forward_error_message_6);
						break;
					case 11:
						toastMsg = getResources().getString(
								R.string.forward_error_message_7);
						break;
					case 12:
						toastMsg = getResources().getString(
								R.string.forward_error_message_8);
						break;
					case 13:
						toastMsg = getResources().getString(
								R.string.forward_error_message_9);
						break;
					default:
						toastMsg = getResources().getString(
								R.string.forward_failed);
					}
					Toast.makeText(mViewManager.getCurrentActivity(), toastMsg,
							Toast.LENGTH_LONG).show();
					if (mProgressDialog != null) {
						mProgressDialog.dismiss();
					}
				} else if (mWeiboType == WeiboType.SohuType) {
					if (mForwardResult == -1) {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(
								mViewManager.getCurrentActivity(),
								getResources().getString(
										R.string.forward_failed),
								Toast.LENGTH_LONG).show();
					} else if (mForwardResult == 400) {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(
								mViewManager.getCurrentActivity(),
								getResources().getString(
										R.string.forward_error_message_9),
								Toast.LENGTH_LONG).show();
					} else {
						sendForwardCound();
					}
				} else if (mWeiboType == WeiboType.NetEaseType) {
					if (mForwardResult == -1) {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(
								mViewManager.getCurrentActivity(),
								getResources().getString(
										R.string.forward_failed),
								Toast.LENGTH_LONG).show();
					} else {
						sendForwardCound();
					}
				} else if (mWeiboType == WeiboType.RenrenType) {
					if (mForwardResult == 1) {
						sendForwardCound();
					} else {
						if (mProgressDialog != null) {
							mProgressDialog.dismiss();
						}
						Toast.makeText(
								mViewManager.getCurrentActivity(),
								getResources().getString(
										R.string.forward_failed),
								Toast.LENGTH_LONG).show();
					}
				}
				break;
			case MSG_GET_TINY_URL_SUCCEED:
				mEditText.setText("["
						+ mTitle.trim()
						+ "]"
						+ mAbs
						+ mTinyUrl
						+ getResources()
								.getString(R.string.forward_baidu_share));
				mEditText.setSelection(mEditText.getText().length());
				mLoadingLayout.setVisibility(View.GONE);
				mForwardLayout.setVisibility(View.VISIBLE);
				if (!mReturnKeyPressed) {
					dispSoftMethodInput(true);
				}
				break;
			case MSG_GET_TINY_URL_FAILED:
				Toast.makeText(
						mViewManager.getCurrentActivity(),
						getResources().getString(
								R.string.forward_open_forward_page_error),
						Toast.LENGTH_SHORT).show();
				goBack();
				break;
			case MSG_GET_FORWARD_COUNT_SUCCEED:
			case MSG_GET_FORWARD_COUNT_FAILED:
				if (mReturnKeyPressed) {
					break;
				} else {
					Toast.makeText(mViewManager.getCurrentActivity(),
							getResources().getString(R.string.forward_succeed),
							Toast.LENGTH_SHORT).show();
					goBack();
				}

				break;
			}
		};
	};

	/**
	 * 初始化
	 */
	private void init() {
		setContentView(R.layout.forwardingview);
		mMyHandleBackButton = true;
		mTextViewHeader = (TextView) findViewById(R.id.textView1);
		mEditTextLayout = (RelativeLayout) findViewById(R.id.layout2);
		mBindingInfo = (TextView) findViewById(R.id.bindingInfo);
		mRemainInputNum = (TextView) findViewById(R.id.remainInputNum);
		mEditText = (EditText) findViewById(R.id.forwardContent);
		mImageView = (ImageView) findViewById(R.id.forwardImage);
		mForwardBtn = (Button) findViewById(R.id.forwardButton);
		mCancelBtn = (Button) findViewById(R.id.cancelButton);
		mLoadingLayout = (RelativeLayout) findViewById(R.id.loadingLayout);
		mForwardLayout = (RelativeLayout) findViewById(R.id.forwardingViewLayout);

		Bundle intent = getIntent().getExtras();
		if (intent != null) {
			mWeiboType = intent.getInt("WeiboType");
			mTitle = intent.getString("Title");
			mImageUrl = intent.getString("ImageUrl");
			Log.i(TAG, "Image url:" + mImageUrl);
			mNewsUrl = intent.getString("NewsUrl");
			mAbs = intent.getString("Abstract");
			mAuthor = intent.getString("Author");
			if (mAbs.length() > MAX_ABSTRACT_LENGTH) {
				mAbs = mAbs.substring(0, MAX_ABSTRACT_LENGTH)
						+ getResources().getString(
								R.string.forward_abstract_suffix);
			}
			Log.i(TAG, "NewsUrl = " + mNewsUrl);
			createTinyUrl(mNewsUrl);
		}

		mRemainInputNum.setText(Integer.toString(MAX_INPUT_NUMBER
				- mEditText.length()));
		initHeaderText();
		initEditTextEvent();
		initBtnEvents();
		initImageViewEvent();
	}

	private void initHeaderText() {
		String forwardTxt = getResources().getString(R.string.forward_text2);
		switch (mWeiboType) {
		case WeiboType.SinaType:
			mTextViewHeader.setText(forwardTxt
					+ getResources().getString(R.string.weibo_sina));
			break;
		case WeiboType.TencentType:
			mTextViewHeader.setText(forwardTxt
					+ getResources().getString(R.string.weibo_tencent));
			break;
		case WeiboType.SohuType:
			mTextViewHeader.setText(forwardTxt
					+ getResources().getString(R.string.weibo_sohu));
			break;
		case WeiboType.NetEaseType:
			mTextViewHeader.setText(forwardTxt
					+ getResources().getString(R.string.weibo_netease));
			break;
		case WeiboType.RenrenType:
			mTextViewHeader.setText(forwardTxt
					+ getResources().getString(R.string.weibo_renren));
			break;
		}
		initScreenName();
	}

	/**
	 * 长链接转为短链接
	 */
	public void createTinyUrl(final String longUrl) {
		NewsService.getTheadPool(true).submit(new Runnable() {
			@Override
			public void run() {
				if (!mReturnKeyPressed) {
					HttpClient httpclient = new DefaultHttpClient();
					HttpPost httppost = new HttpPost(CREATE_TINY_URL);
					try {
						Log.i(TAG, "createTinyUrl longUrl = " + longUrl);
						List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
						nameValuePairs.add(new BasicNameValuePair("url",
								longUrl));
						httppost.setEntity(new UrlEncodedFormEntity(
								nameValuePairs));
						HttpResponse response;
						httpclient.getParams().setParameter(
								CoreConnectionPNames.CONNECTION_TIMEOUT,
								HTTP_CONNECTION_TIMEOUT);
						response = httpclient.execute(httppost);
						String res = EntityUtils.toString(response.getEntity());
						try {
							JSONObject root = new JSONObject(res);
							mTinyUrl = root.getString("tinyurl");
							Log.i(TAG, "mTinyUrl = " + mTinyUrl);
							mHandler.sendEmptyMessage(MSG_GET_TINY_URL_SUCCEED);
							return;
						} catch (JSONException e) {
							Log.i(TAG, "createTinyUrl json parse error");
							e.printStackTrace();
						}
					} catch (ClientProtocolException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					mHandler.sendEmptyMessage(MSG_GET_TINY_URL_FAILED);
				}
			}
		});
	}

	/**
	 * 发送转发计数
	 */
	public void sendForwardCound() {
		if (mTitle.equals("") || mNewsUrl.equals("") || mAuthor.equals("")) {
			goBack();
			return;
		}

		NewsService.getTheadPool(true).submit(new Runnable() {
			@Override
			public void run() {
				String url = String.format(GET_FORWARD_COUNT_URL
						+ "m=app&v=forward_count&source=%s&title=%s&url=%s",
						URLEncodeUtils.encodeURL(mAuthor.trim()),
						URLEncodeUtils.encodeURL(mTitle.trim()),
						mNewsUrl.trim());
				try {
					String res = Networking.downloadString(url);
					if (res.contains("<count>"))
						;
					{
						mHandler.sendEmptyMessage(MSG_GET_FORWARD_COUNT_SUCCEED);
						return;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				mHandler.sendEmptyMessage(MSG_GET_FORWARD_COUNT_FAILED);
			}
		});
	}

	/**
	 * 按键响应
	 */
	public boolean dispatchKeyEvent(KeyEvent event) {
		mRemainInputNum.setText(Integer.toString(MAX_INPUT_NUMBER
				- mEditText.length()));
		return super.dispatchKeyEvent(event);
	}

	/**
	 * 输入内容为空提示对话框
	 */
	private void remindDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				ForwardingView.this);
		builder.setMessage(getBaseContext().getString(
				R.string.forward_dialog_no_input_msg));
		builder.setTitle(getBaseContext().getString(
				R.string.forward_dialog_title));

		builder.setPositiveButton(
				getBaseContext().getString(
						R.string.forward_dialog_button_confirm),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOADING_DIALOG_KEY: {
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setMessage(getResources().getString(
					R.string.forward_loading_dialog_message));
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.setCancelable(true);
			return mProgressDialog;
		}
		}
		return null;
	}

	private void initBtnEvents() {
		mForwardBtn.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					// mForwardBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_button_function_selected));
				} else {
					// mForwardBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_button_function));
				}
			}
		});

		mForwardBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				mForwardText = mEditText.getText().toString().trim();
				if (mForwardText.equals("")) {
					remindDialog();
					return;
				}
				if (!NetUtil.checkNet(ForwardingView.this)) {
					Toast.makeText(mViewManager.getCurrentActivity(),
							getResources().getString(R.string.app_no_network),
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (mBindItem == null) {
					goBack();
				} else {
					showDialog(LOADING_DIALOG_KEY);
					NewsService.getTheadPool(true).submit(new Runnable() {
						@Override
						public void run() {
							if (mReturnKeyPressed) {
								return;
							}
							Weibo weibo = Weibo.getInstance(mWeiboType);
							weibo.setToken(mBindItem.getAccessToken(),
									mBindItem.getTokenSecret());
							if (weibo.mAccessToken == null)
								weibo.mAccessToken = new AccessToken(mBindItem
										.getAccessToken(), mBindItem
										.getTokenSecret());

							if (mWeiboType == WeiboType.SinaType
									|| mWeiboType == WeiboType.SohuType) {
								mImageItem = WeiboUtils.GetImageItem(mImageUrl);
								if (mImageItem != null) {
									try {
										mForwardResult = weibo.uploadStatus(
												mForwardText, mImageItem);
									} catch (Exception e) {
									}
								} else {
									mForwardResult = weibo
											.publishMsg(mForwardText);
								}
							} else if (mWeiboType == WeiboType.TencentType) {
								mImageItem = WeiboUtils.GetImageItem(mImageUrl);
								if (mImageItem != null) {
									try {
										mForwardResult = weibo
												.TencentUploadStatus(
														mForwardText,
														mImageItem);
									} catch (Exception e) {
									}
								} else {
									mForwardResult = weibo
											.publishMsg(mForwardText);
								}
							} else if (mWeiboType == WeiboType.NetEaseType) {
								try {
									File file = WeiboUtils.GetFile(
											ForwardingView.this, mImageUrl);
									if (file != null) {
										String uploadImageUrl = weibo
												.updateImage(file);
										if (uploadImageUrl != null) {
											if (mBindItem != null)
												mForwardResult = weibo
														.publishMsg(mForwardText
																+ uploadImageUrl);
										}
									} else {
										mForwardResult = weibo
												.publishMsg(mForwardText);
									}
								} catch (WeiboException e) {
									e.printStackTrace();
								}
							} else if (mWeiboType == WeiboType.RenrenType) {
								mImageItem = WeiboUtils.GetImageItem(mImageUrl);
								Renren renren = new Renren();
								renren.setAccessToken(mBindItem
										.getAccessToken());
								if (mImageItem == null) {
									mForwardResult = renren
											.updateStatus(mForwardText);
								} else {
									mForwardResult = renren.updateStatus(
											mForwardText,
											mImageItem.getContent());
								}
							}
							Log.i(TAG, "mForwardResult=" + mForwardResult);
							mHandler.sendEmptyMessage(MSG_FORWARD_FINISH);
						}
					});
				}
			}
		});

		mCancelBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				goBack();
			}
		});
		mCancelBtn.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					// mCancelBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_button_function_selected));
				} else {
					// mCancelBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_button_function));
				}
			}
		});
	}

	/**
	 * 点击图片响应
	 */
	private void initImageViewEvent() {
		mImageView.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						ForwardingView.this);
				builder.setMessage(getResources().getString(
						R.string.forward_delete_pic));
				builder.setTitle(getResources().getString(
						R.string.forward_dialog_title));
				builder.setPositiveButton(
						getResources().getString(
								R.string.forward_dialog_button_yes),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mImageUrl = "";
								mImageView.setVisibility(View.GONE);
								dialog.dismiss();
							}
						});

				builder.setNegativeButton(
						getResources().getString(
								R.string.forward_dialog_button_no),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				builder.create().show();
			}
		});
	}

	/**
	 * 显示绑定用户名
	 */
	private void initScreenName() {
		BindingDBEngine mEngine = new BindingDBEngine(ForwardingView.this);
		mBindItem = mEngine.getBindItem(mWeiboType);
		if (mBindItem == null) {
			mBindingInfo.setTextColor(R.color.color_forward_unbind_text);
			mBindingInfo.setText(getResources().getString(
					R.string.forward_unbinding));
		} else {
			mBindingInfo.setTextColor(R.color.color_forward_bind_text);
			mBindingInfo.setText(mBindItem.getScreenName());
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		mViewManager.setTitle(getString(R.string.app_forwardnews));
		mViewManager.hideLeftButton();
		mViewManager.setTabBarVisibility(false);
		mViewManager.setStatus("");
		
		if (mImageUrl != null) {
			if (mImageUrl.equals("")) {
				return;
			}
			mImagePath = getCacheDir() + HEAD_IMAGE_SAVE_PATH
					+ UrlUtils.getNameFromUrl(mImageUrl);
			final File file = new File(mImagePath);
			if (file.exists()) {
				mHandler.sendEmptyMessage(MSG_REFRESH_IMAGE_VIEW);
			} else {
				File savePath = new File(getCacheDir() + HEAD_IMAGE_SAVE_PATH);
				if (!savePath.exists()) {
					savePath.mkdirs();
				}
				Downloader.getInstance().download(mImageUrl,
						getCacheDir() + HEAD_IMAGE_SAVE_PATH,
						new DownloadCallback() {
							@Override
							public void onFinished(DownloadTask finishedTask) {
								if (file.exists()) {
									mHandler.sendEmptyMessage(MSG_REFRESH_IMAGE_VIEW);
								}
							}

							@Override
							public void onFailed() {
							}
						});
			}
		}
	}

	/**
	 * 页面结束返回
	 */
	public void goBack() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
		dispSoftMethodInput(false);
		ViewManager.getInstance().showView(moldViewId, mOldView, false, null);
	}

	private void initEditTextEvent() {
		mEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence text, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence text, int start,
					int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable edit) {
				mRemainInputNum.setText(Integer.toString(MAX_INPUT_NUMBER
						- mEditText.length()));
			}
		});
		mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					mEditTextLayout
							.setBackgroundDrawable(getResources()
									.getDrawable(
											R.drawable.image_forward_textfield_selected));
				} else {
					mEditTextLayout.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.image_forward_textfield));
				}
			}
		});
	}

	/**
	 * 显示隐藏软键盘
	 */
	private void dispSoftMethodInput(boolean isDisp) {
		InputMethodManager imm = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (isDisp) {
			imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		} else {
			imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			mReturnKeyPressed = true;
			goBack();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		dispSoftMethodInput(false);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mImageView.destroyDrawingCache();
		dispSoftMethodInput(false);
	}

	public static void setOldView(final String oldViewId, Class<?> oldViewClass) {
		moldViewId = oldViewId;
		mOldView = oldViewClass;
	}
}
